package com.csu.letaobackend.controllerM;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.commom.KdApiExpressSearchDemo;
import com.csu.letaobackend.service.OrderService;
import com.csu.letaobackend.serviceM.CatalogMService;
import com.csu.letaobackend.serviceM.OrderMService;
import com.csu.letaobackend.vo.CategoryVO;
import com.csu.letaobackend.vo.OrderVO;
import com.csu.letaobackend.vo.RefundserviceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/admin/order")
public class OrderMController {

      @Autowired
      OrderMService orderMService;

      @GetMapping("/orders/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<OrderVO>> getAllOrders(@PathVariable int pageid, @PathVariable int pagesize, @RequestParam int status) {

            return orderMService.getAllOrders(pageid, pagesize, status);
      }

      @GetMapping("/orders/{orderid}/detail")
      @ResponseBody
      public CommonResponse<OrderVO> getOrderByOrderId(@PathVariable String orderid) {

            return orderMService.getOrderByOrderId(orderid);
      }

      @GetMapping("/orders/refund/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<RefundserviceVO>> getOrderByOrderId(@PathVariable int pageid, @PathVariable int pagesize) {

            return orderMService.getRefundservices(pageid, pagesize);
      }

      @GetMapping("/orders/refund/{orderid}")
      @ResponseBody
      public CommonResponse getRefundservice(@PathVariable String orderid) {

            return orderMService.getRefundservice(orderid);
      }

      @PostMapping("/orders/{orderid}/confirmreturn")
      @ResponseBody
      public CommonResponse confirmreturnOrder(@PathVariable String orderid, HttpSession httpSession) {
            orderMService.updateOrderStatus(orderid, 5);
            orderMService.refuseRefundservice(orderid,"同意退货");
            return CommonResponse.createForSuccessMessage("同意退货成功");
      }

      @PostMapping("/orders/{orderid}/refusereturn")
      @ResponseBody
      public CommonResponse refusereturnOrder(@PathVariable String orderid, @RequestParam String refusalreason, HttpSession httpSession) {
            orderMService.updateOrderStatus(orderid, 6);
            orderMService.refuseRefundservice(orderid,refusalreason);
            return CommonResponse.createForSuccessMessage("拒绝退货成功");
      }

      @GetMapping("/delivery/info")
      @ResponseBody
      public CommonResponse getdeliveryinfo(){
            try {
                  KdApiExpressSearchDemo api = new KdApiExpressSearchDemo();
                  return CommonResponse.createForSuccess(api.orderOnlineByJson());
            } catch (Exception e) {
                  return CommonResponse.createForError("查询失败");
            }
      }
}
